#include "BasicGameStateParser.h"
#include "..\utils\PlainTextReader.h"
#include "..\addons\BasicGameState.h"

namespace ds {

BasicGameStateParser::BasicGameStateParser(void)
{
}


BasicGameStateParser::~BasicGameStateParser(void)
{
}

bool BasicGameStateParser::readTextFile(const char* name) {
	PlainTextReader reader;
	if ( reader.readFile(name) ) {
		Category* root = reader.getRoot();
		std::string name = root->getProperty("name");
		std::string material = root->getProperty("material");
		std::string layer = root->getProperty("layer");
		BasicGameState* bgs = new BasicGameState(name,material,layer);
		if ( root->hasProperty("next_state") ) {
			bgs->setNextStateName(root->getProperty("next_state"));
		}
		bool active = root->getBool("active",false);		
		std::vector<Category*> children = root->getChildren();
		for ( size_t i = 0; i < children.size(); ++i ) {
			Category* sub = children[i];
			if ( sub->getName() == "text_node" ) {
				std::string nodeName = sub->getProperty("name");
				std::string font = sub->getProperty("font");
				bgs->createTextNode(nodeName,font);
			}
			else if ( sub->getName() == "text" ) {
				std::string node = sub->getProperty("text_node");
				Vec2 pos = sub->getVec2("pos");
				std::string txt = sub->getProperty("text");
				int index = sub->read<int>("index",-1);
				bgs->addText(node,pos,txt,index);
			}
			else if ( sub->getName() == "image" ) {
				Vec2 pos = sub->getVec2("pos");
				Rect rect = sub->getRect("rect");
				bgs->addSprite(pos,rect);
			}
		}
		bgs->setActive(active);
		gEngine->addGameState(bgs);
	}
	return true;
}

}
